package com.balabala.gym_user.controller.admin;


import cn.hutool.core.date.DateUtil;

import com.balabala.dto.UserNameDto;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_user.pojo.dto.UserQueryDTO;
import com.balabala.gym_user.pojo.entity.User;
import com.balabala.gym_user.service.UserService;
import com.balabala.result.Result;
import com.balabala.vo.UserNameVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author 何涛
 * @Date: 2023/04/30/ 13:10
 * @description 用户后台管理接口
 */
@RestController
@RequestMapping("/admin/user")
@CrossOrigin
@Slf4j
@Api(tags = "用户后台管理接口")
public class UserAdminController {
    @Autowired
    UserService userService;
    @PostMapping("/getUserPageList")
    @ApiOperation("用户分页信息")
    public Result getUserPageList(@RequestBody UserQueryDTO userQueryDto)
    {
        PageInfo pageInfo=userService.getUserPageList(userQueryDto);
        return Result.ok(pageInfo.getList(),pageInfo.getTotal());
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result findById(@PathVariable Long id)
    {

        User user = userService.findById(id);
        user.setBirthdayDate(DateUtil.format(user.getBirthday(),"yyyy-MM-dd"));
        user.setCreateDate(DateUtil.format(user.getCreateTime(), "yyyy-MM-dd"));
        return Result.ok(ResultEnum.GENERALLY_SUCCESS,user);
    }
    @ApiOperation("修改用户详细信息")
    @PutMapping("updateUserDetailInfo")
    public Result getUserDetailInfo(@RequestBody User user)
    {
        log.info("正在修改用户信息，新的用户信息：{}",user);
        return Result.judge(userService.updateById(user));
    }

    @ApiOperation("根据id删除用户个人信息")
    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Long id)
    {
        log.info("删除用户信息，id:{}",id);
        return Result.judge(userService.removeById(id));
    }

    @ApiOperation("批量删除用户信息")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids)
    {
        log.info("批量删除用户，ids：{}",ids);
        return Result.judge(userService.removeByIds(ids));
    }

    @ApiOperation("根据条件查询用户会员卡id")
    @PostMapping("/findVipIdBy")
    public Result findVipIdBy(@RequestBody UserNameDto userNameDto)
    {
        log.info("传入的用户名，userNameDto：{}",userNameDto);
        List<UserNameVo> vidByName = userService.findVidByName(userNameDto);
        return Result.ok(vidByName);
    }

}
